package com.lwx.chapter5;

public class LinkedList<T> {
    private class Node{
        T e;
        Node next;
        public Node(){
            this.e = null;
            this.next = null;
        }
        public Node(T e){
            this.e = e;
            this.next = null;
        }

        public Node(T e, Node node){
            this.e = e;
            this.next = node;
        }
    }
    //头结点是空的，不含有任何元素
    private Node dummyHead;
    private int size;
    public LinkedList(){
        dummyHead = new Node(null,null);
        size = 0;
    }

    public LinkedList(T[] arr){
        this();
        for (int i = 0; i < arr.length; i++) {
            add(i,arr[i]);
        }
    }

    public int getSize(){
        return size;
    }

    public boolean isEmpty(){
        return size == 0;
    }



    /**
     * 在索引为index(0-infanity)的位置插入元素,从第0个位置开始插入
     * @param index
     */
    public void add(int index, T e){
        if(index < 0|| index > size){
            throw new IllegalArgumentException("Added failed.Illegal index");
        }
        Node prev = dummyHead;
        //找到要插入位置的前一个位置,因为要从虚拟头结点开始遍历，对于用户来说，虚拟头结点是不被暴露的,dummyHead是0索引的前一个位置的节点
        for (int i = 0; i < index; i++) {
            prev = prev.next;
        }
//            Node node = new Node(e);
//            node.next = prev.next;
//            prev.next = node;
        prev.next = new Node(e, prev.next);
        size ++;
    }

    /**
     * 采用头插法
     * @param e
     */
    public void addFirst(T e){
        add(0,e);
    }

    public void addLast(T e){
        add(size, e);
    }


    /**
     * 获得第index（0，size）的数据
     * @param index
     * @return
     */
    public T get(int index){
        if(index < 0|| index >= size){
            throw new IllegalArgumentException("Get failed.Illegal index");
        }
        Node cur = dummyHead.next;
        for (int i = 0; i < index; i++) {
            cur = cur.next;
        }
        return cur.e;
    }

    /**
     * 更新第index（0，based）个位置的元素
     * @param index
     * @param e
     */
    public void set(int index, T e){
        if(index < 0|| index >= size){
            throw new IllegalArgumentException("Get failed.Illegal index");
        }
        Node cur = dummyHead.next;
        for (int i = 0; i < index; i++) {
            cur = cur.next;
        }
        cur.e = e;
    }

    public boolean contains(T e){
        Node cur = dummyHead.next;
        while (cur != null){
            if(cur.e.equals(e)){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    /**
     * 删除链表中的第一个元素e
     * @param e
     * @return
     */
    public boolean delete(T e){
        Node pre = dummyHead;
        Node cur = dummyHead.next;
        while (cur != null){
            if(cur.e.equals(e)){
                pre.next = cur.next;
                //便于节点回收
                cur.next = null;
                size --;
                cur = pre.next;
                continue;
            }
            pre = cur;
            cur = cur.next;
        }
        if(!contains(e)){
            return true;
        }
        return false;
    }

    /**
     * 删除索引为（0，index）的元素
     * @param index 被删除的元素
     * @return
     */
    public T remove(int index){
        if(index < 0|| index >= size){
            throw new IllegalArgumentException("Delete failed.Illegal index");
        }
        Node prev = dummyHead;
        for (int i = 0; i < index; i++) {
            prev = prev.next;
        }
        Node delNode = prev.next;
        prev.next = delNode.next;
        delNode.next = null;
        size --;
        return delNode.e;
    }

    public void show(){
        if(dummyHead.next == null){
            return;
        }
        Node node = dummyHead;
        for (int i = 0; i < size-1; i++) {
            node = node.next;
            System.out.print(node.e+"->");
        }
        node = node.next;
        if(node.next == null){
            System.out.println(node.e);
        }
    }

    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        Node cur = dummyHead.next;
        while (cur != null){
            res.append(cur.e+"->");
            cur = cur.next;
        }
        res.append("NULL");
        return res.toString();
    }
}
